<?php
    class user
    {
        //авторизация
        public static function  login()
        {
            $ac = m::json_data_read('conf.dat');
            
            if ( isset($ac['login']) and  isset($ac['pass']))
            {
                
            }
            else
            {
                return 0;
            }
            
            if ( isset( $_SESSION['ba_user_id'] ) ) 
            {
                return 1;
            }
            
            if ( isset($_POST['ba_login']) && isset($_POST['ba_password']) )
            {
                $login = trim ($_POST['ba_login']);
                $password = trim($_POST['ba_password']);
                
                $password = md5 ( md5 ( md5 ( $password ) ) ) ;  

                // если такой пользователь нашелся
                if ( $login == $ac['login'] &&  $password == $ac['pass'] ) 
                {
                   // $row = db::fetch_array($sql);
                    $_SESSION['ba_user_id'] = 1;

                    @ setcookie('ba_login', $login, time() + 2592000, "/");
                    @ setcookie('ba_password', $password, time() + 2592000, "/");
                    
                    return 1;
                }
                else 
                {
                    return 0;
                }    

            }

            if ( !isset( $_SESSION['ba_user_id'] ) ) 
            {
                // то проверяем его куки
                // вдруг там есть логин и пароль к нашему скрипту

                if (isset($_COOKIE['ba_login']) && isset( $_COOKIE['ba_password'] ) ) 
                {
                    if ( $_COOKIE['ba_login'] == $ac['login'] &&  $_COOKIE['ba_password'] == $ac['pass'] ) 
                    {
                        $_SESSION['ba_user_id'] = 1;
                        
                        return true;
                    }
                }
                else
                {
                    //include( PAGE_PATH . 'login.php');
                }
            }
            else
            {
                return true;
            }
        }

        //выходим 
        public static function  logoff()
        {
            unset( $_COOKIE['ba_login'] );
            unset( $_COOKIE['ba_password'] );
            unset( $_SESSION['ba_user_id'] );
            setcookie('ba_login', '', 0, "/");
            setcookie('ba_password', '', 0, "/");

            m::r ('index.php');
        }
    }
?>